package winOrDie.tests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import winOrDie.GameEngine.Client.WoD_Client_Test;
import winOrDie.GameEngine.Server.WoD_Server_Test;
import winOrDie.Net.WoD_Net_Protocol_ASCII;

public class Net_test {

	private static WoD_Server_Test woDServerTest = null;
	private static WoD_Client_Test woDClientTest = null;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/*
		 * Usage:
		 *  Net_test s tcp_port udp_port client_udp_port
		 *  Net_test c serverHost tcp_port_server PlayerName backup_port client_udp_port
		 */
		
		System.setProperty("java.net.preferIPv4Stack", "true");
		
		
		if (args[0].equals("s"))
		{
			int tcp_port = Integer.valueOf(args[1]);
			int udp_port = Integer.valueOf(args[2]);
			int client_udp_port = Integer.valueOf(args[3]);
			
			woDServerTest = new WoD_Server_Test();
			woDServerTest.setPort_TCP_CONTROL(tcp_port);
			woDServerTest.setPort_UDP_RT(udp_port);
			woDClientTest = new WoD_Client_Test();
			
			
			final boolean run_sntp = true; 
			
			try {
				woDServerTest.start_Server();
				
				Thread.sleep(1000);
			
				woDClientTest.setUsername("Server");
				woDClientTest.setUdp_RT_Port(client_udp_port);
				woDClientTest.connectToWoDServer("127.0.0.1", tcp_port);
				
				Thread.sleep(2000);
				
				
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						
						while (run_sntp) {
							woDServerTest.send_NTP_Messages();
							try {
								Thread.currentThread().sleep(8000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
					}
				});
				thread.start();
				
				chat();
				
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
			
		}
		
		else if (args[0].equals("c"))
		{
			String hostName = args[1];
			int tcp_port = Integer.valueOf(args[2]);
			String PlayerName = args[3];
			int backup_port = Integer.valueOf(args[4]);
			int client_udp_port = Integer.valueOf(args[5]);
			
			woDClientTest = new WoD_Client_Test();
			woDClientTest.setUsername(PlayerName);
			woDClientTest.setUdp_RT_Port(client_udp_port);
			woDClientTest.setTcp_backup(backup_port);
			
			try {
				
				woDClientTest.connectToWoDServer(hostName, tcp_port);
				Thread.sleep(1000);
				chat();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
		
		
		
	}

	
	
	private static void chat()
	{
		// open a reader over the keyboard (System.in) stream.
		BufferedReader reader= new BufferedReader(
		new InputStreamReader(System.in)); 
		
		boolean exit_chat = false;
		
		String input_Text = null;
		while (!exit_chat) {
			try {
				input_Text = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				exit_chat = true;
			}
			String Chat_Message = WoD_Net_Protocol_ASCII.GameRoom.Chat + " " + input_Text; 
			try {
				woDClientTest.getWoDClientControlChannel().sendData(Chat_Message);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				exit_chat = true;
			}
		}
		
	}
	
}
